﻿using Unity.VisualScripting;
using static CyberU;
using static CyberU.UI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class ScrollWheel : MonoBehaviour
{
    public delegate void ScrollAction(float delta);

    public event ScrollAction OnScroll;

    public static ScrollWheel Create(object parent, Action<float> onScroll)
    {
        GameObject gameObject = AsGameObject(parent);
        ScrollWheel scrollWheel = GetOrAddComponent<ScrollWheel>(gameObject);
        scrollWheel.Add(onScroll);
        return scrollWheel;
    }

    public void Add(Action<float> onScroll)
    {
        OnScroll += onScroll.Invoke; //隐式转换
    }

    private void Update()
    {
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (scroll != 0)
        {
            OnScroll?.Invoke(scroll);
        }
    }
}